﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace WebPresentation
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //    if (Session["UserID"] != null)
            //    {
            //        if ((string)Session["Role"] == "Admin")
            //        {
            //            Response.Redirect("~/Document.aspx");
            //        }
            //        else
            //        {
            //            Response.Redirect("~/DocumentUser.aspx");
            //        }
            //    }
            //}
        }

        protected void btnlogin_Click(object sender, ImageClickEventArgs e)
        {
            
            DataTable u = new UserBO().GetByAuthentication(txtname.Text.Trim(), txtpass.Text);
            if (u.Rows.Count > 0)
            {
                Session["UserID"] = u.Rows[0]["uID"].ToString();
                Session["Username"] = u.Rows[0]["FullName"].ToString();
                Session["Role"] = u.Rows[0]["rName"].ToString();
                Session["UserDepartment"] = u.Rows[0]["depCode"].ToString();
                Session["UserDepartmentID"] = u.Rows[0]["depID"].ToString();

                if ((string)Session["Role"] == "Admin")
                {
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    Response.Redirect("~/HomeUser.aspx");
                }
            }
            else
            {
                Session["UserID"] = null;
                Session["Username"] = null;
                Session["Role"] = null;
                Session["UserDepartment"] = null;
                Session["UserDepartmentID"] = null;
                btnlogin.PostBackUrl = "~/LoginPage.aspx";
            }          
        }
    }
}